﻿//1.1预加载效果
var v_pub_preloadingimg = "<div style='' align='center'><img style='width:15px;height:15px;'  src=\"/images/loadingsmall.gif\" title=\"loading...\" /></div>";
var AppConst_PleaseSelectString = "--- 请选择 ---";
function getPageScroll() {
    var yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        //xScroll = self.pageXOffset; 
    } else if (document.documentElement && document.documentElement.scrollTop) {
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {
        yScroll = document.body.scrollTop;
    }
    //arrayPageScroll = new Array('',yScroll)
    return yScroll;
}
//分页当前页码
var CurrPage = 0;

//如果回调方法是空，这用这个方法代替
function nullfun() { }

//#region 显示屏幕下方等待信息
//底栏加载等待
var gWaitingListB = [];
function gfShowWaitingB() {
    var dvBg1 = document.getElementById("waitDivB");
    if (null != dvBg1) {
        //document.body.removeChild(dvBg);
    }
    else {
        var dvMsg = document.createElement("div");

        var dc = _gfDocumentBounds();

        //dc.ch = gfViewportHeight();
        dvMsg.style.position = "fixed";
        dvMsg.setAttribute('id', 'waitDivB');
        dvMsg.style.width = (dc.cw - 10) + "px"; // "280px";
        dvMsg.style.height = "48px";
        dvMsg.style.top = dc.ch + "px";  //h - 50;
        dvMsg.style.left = "0px"; // "50%";
        dvMsg.style.marginTop = "-50px";
        dvMsg.style.marginLeft = "5px"; // "-140px";
        dvMsg.style.zIndex = "9999";
        dvMsg.style.opacity = 0.5;
        dvMsg.style.background = "gray";
        dvMsg.style.filter = "alpha(opacity=80)";
        // width='50' height='50'
        var html = "<center><img src='/images/loadingsmall.gif'><span style='font-weight:bold;font-size:10pt;'></span></center>";

        dvMsg.innerHTML = html;
        document.body.appendChild(dvMsg);
        if ($.browser.safari) {
            $('#msgDivB').css('top', window.innerHeight + window.scrollY + "px");
            //            $("#msgDivB").everyTime("200ms", 'gotop1', function () {
            //                $('#msgDivB').css('top', window.innerHeight + window.scrollY + "px");
            //            });
        }
    }
    gWaitingListB.push({ show: 1, close: 0, wait: 0 });

}
function gfHideWaitingB(beforeStop, funname) {
    for (var k = gWaitingListB.length - 1; k >= 0; k--) {
        var p = gWaitingListB[k];
        if (p.close > 0) {
            p.wait = 1;
            gWaitingListB.splice(k, 1);
            break;
        }
        else {
            p.close = 1;
            setTimeout("gfHideWaitingB()", 300);
            break;
        }
        //}
    }
    if (gWaitingListB.length == 0) {
        var dvMsg = document.getElementById("waitDivB");
        if ($.browser.safari) {
            //$("#msgDivB").stopTime('gotop1');  //我们基本不用safari的浏览器，可以注释
        }
        if (null != dvMsg) {
            document.body.removeChild(dvMsg);
        }
        dvMsg = document.getElementById("msgDivB");
        if (null != dvMsg) {
            document.body.removeChild(dvMsg);
        }
    }
    else {
        setTimeout("gfHideWaitingB()", 45000); //最多不过45秒
    }
}


// #region _gfDocumentBounds
function _gfDocumentBounds() {
    if (document.documentElement && document.compatMode == "CSS1Compat") {
        return {
            st: document.documentElement.scrollTop,
            sl: document.documentElement.scrollLeft,
            sw: document.documentElement.scrollWidth,
            sh: document.documentElement.scrollHeight,
            cw: document.documentElement.clientWidth,
            ch: document.documentElement.clientHeight
        }
    } else if (document.body) {
        return {
            st: document.body.scrollTop,
            sl: document.body.scrollLeft,
            sw: document.body.scrollWidth,
            sh: document.body.scrollHeight,
            cw: $(window.parent.document).find("#m_iframe") ? parent.document.body.clientWidth : document.body.clientWidth,
            ch: $(window.parent.document).find("#m_iframe") ? parent.document.body.scrollHeight : document.body.clientHeight
        }
    }
}
function gfViewportHeight() {
    var height = 0;
    if (window.innerHeight) {
        height = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        height = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    }
    return height;

}
// #endregion
//#endregion

//#region 显示全屏加载信息
//全屏加载等待
var gWaitingList = [];
function gfShowWaiting(opt, funname) {
    //if ($("#msglist").length == 0) {
    //    $("body").append("<div id='msglist'></div>");
    // }
    var caller = (funname == undefined || funname == null ? "noname" : funname);
    var msg = "", timer, stopBtnEnable = false, beforeStop = undefined;
    if (opt) {
        if (opt.message) {
            msg = opt.message;
        }
        if (opt.timer) {
            timer = opt.timer;
        }
        if (opt.stopBtnEnable) {
            stopBtnEnable = opt.stopBtnEnable;
        }
        if (opt.beforeStop) {
            beforeStop = opt.beforeStop;
        }
    }
    var dvBg1 = document.getElementById("waitDiv");
    if (null != dvBg1) {
        //document.body.removeChild(dvBg);
    }
    else {
        var dvBg = document.createElement("div");
        var dvMsg = document.createElement("div");

        var h = Math.max(
			document.documentElement.scrollHeight,
			document.body.scrollHeight
		);
        var w = Math.max(
			document.documentElement.scrollWidth,
			document.body.scrollWidth
		);

        dvBg.setAttribute('id', 'waitDiv');

        dvBg.style.height = h + "px";
        dvBg.style.width = w + "px";
        dvBg.style.zIndex = "9999";
        dvBg.style.top = 0;
        dvBg.style.left = 0;

        dvBg.style.background = "#A6BACC";
        dvBg.style.opacity = 0.5;
        dvBg.style.filter = "alpha(opacity=50)";

        dvBg.style.position = "absolute";
        document.body.appendChild(dvBg);


        dvMsg.style.position = "absolute";
        dvMsg.setAttribute('id', 'msgDiv');
        dvMsg.style.width = "280px";
        dvMsg.style.top = "50%";
        dvMsg.style.left = "50%";
        dvMsg.style.marginTop = "-50px";
        dvMsg.style.marginLeft = "-140px";
        dvMsg.style.zIndex = "1112";
        dvMsg.style.opacity = 0.5;
        dvMsg.style.filter = "alpha(opacity=50)";

        var html = "<center><img src='/images/loading.gif'><span style='font-weight:bold;font-size:10pt;'>";
        if (msg) {
            html += msg;
        }
        html += "</span>";
        if (stopBtnEnable) {
            html += "<br/><input type='button' value='Stop' onclick='stopWait(" + beforeStop + ");'>";
        }
        html += "</center>";

        dvMsg.innerHTML = html;

        document.body.appendChild(dvMsg);
    }
    //if(gWaitingCount<=0) gWaitingCount = 1
    //else gWaitingCount++;
    gWaitingList.push({ name: caller, show: 1, close: 0, wait: 0 });
    //$("#msglist").append("<div>show:"+jsonToString(gWaitingList)+'</div>');
    //$("#msglist").append("<div>show:gWaitingCount:" + gWaitingCount + ' gWaitingClosing:' + gWaitingClosing + "</div><div class='clear'></div>");
    //if (gWaitingClosing > 0) gWaitingClosing --;

    if (timer && !isNaN(timer)) {
        if (beforeStop == undefined)
            setTimeout("gfHideWaiting(undefined,'" + funname + "')", timer);
        else
            setTimeout("gfHideWaiting(" + beforeStop + ",'" + funname + "')", timer);
    }
    if (gWaitingList.length == 1) {
        setTimeout("gfHideWaiting(undefined,'" + funname + "')", 45000); //最多不过45秒
    }
}
function gfHideWaiting(beforeStop, funname) {
    var caller = (funname == undefined || funname == null ? "noname" : funname);
    for (var k = gWaitingList.length - 1; k >= 0; k--) {
        var p = gWaitingList[k];
        //        if (p.name == caller) {
        if (p.close > 0) {
            p.wait = 1;
            gWaitingList.splice(k, 1);
            break;
        }
        else {
            p.close = 1;
            if (beforeStop == undefined || beforeStop == null) {
                setTimeout("gfHideWaiting(undefined,'" + funname + "')", 300);
            }
            else {
                setTimeout("gfHideWaiting(" + beforeStop + ",'" + funname + "')", 300);
            }
            //$("#msglist").append("<div>close wait:gWaitingCount:" + gWaitingCount + ' gWaitingClosing:' + gWaitingClosing + "</div><div class='clear'></div>");
            break;
        }
        //}
    }
    //$("#msglist").append("close:done "+jsonToString(gWaitingList));
    if (gWaitingList.length == 0) {
        var dvBg = document.getElementById("waitDiv");
        if (null != dvBg) {
            if (beforeStop) {
                beforeStop();
            }
            document.body.removeChild(dvBg);
        }
        var dvMsg = document.getElementById("msgDiv");
        if (null != dvMsg) {
            document.body.removeChild(dvMsg);
        }
        //$("#msglist").append("<div>close comp:gWaitingCount:" + gWaitingCount + ' gWaitingClosing:' + gWaitingClosing + "</div><div class='clear'></div>");
    }
}
//#endregion


//#region 打印方法
$.extend({
    print: function (eventTarget, eventArgument) {
        window.print();
        //window.opener.__doPostBack(eventTarget, eventArgument);
        //window.close();

        jQuery(document).ready(function () {
            setTimeout(function () { window.close(); }, 1);
        });
    },
    printagain: function (eventTarget, eventArgument) {
        if (confirm("重复打印！")) {
            window.print();
            //window.opener.__doPostBack(eventTarget,eventArgument);
            //window.close();

            jQuery(document).ready(function () {
                setTimeout(function () { window.close(); }, 1);
            });
        }
        else {
            window.close();
        }
    }
});

//#endregion

//#region 列表加载显示
$.fn.ListWaiting = function () {
    $(this).empty().html(v_pub_preloadingimg);
}
//#endregion
//#region 全局回车搜索注册方法
$.extend({
    EnterAutoSearch: function (TriggerIds, SearchId) {

        SearchId = SearchId || "search";
        //alert(TriggerIds.length);
        TriggerIds = TriggerIds || []; //默认值
        for (var i = 0; i < TriggerIds.length; i++) {
            //绑定回车查询事件
            $("#" + TriggerIds[i]).keydown(function (event) {
                if (13 == event.keyCode) {
                    $("#" + SearchId).click(); //查询
                    return false;
                }
            });
        }
    },
    EnterAutoSearch1: function (TriggerIds, SearchId) {

        SearchId = SearchId || "search1";
        //alert(TriggerIds.length);
        TriggerIds = TriggerIds || []; //默认值
        for (var i = 0; i < TriggerIds.length; i++) {
            //绑定回车查询事件
            $("#" + TriggerIds[i]).keydown(function (event) {
                if (13 == event.keyCode) {
                    $("#" + SearchId).click(); //查询
                    return false;
                }
            });
        }

    }
});
//#endregion
//#region 扩展trim方法
//String.prototype.trim = function () { return $.trim(this); }
//#endregion

//#region 表单序列化函数
function convertArray(o) { //主要是推荐这个函数。它将jquery系列化后的值转为name:value的形式。
    var v = {};
    for (var i in o) {
        if (typeof (v[o[i].name]) == 'undefined') v[o[i].name] = o[i].value;
        else v[o[i].name] += "," + o[i].value;
    }
    return v;
}
//#endregion 

//#region 格式替换函数
$.format = function (source, params) {
    if (arguments.length == 1)
        return function () {
            var args = $.makeArray(arguments);
            args.unshift(source);
            return $.format.apply(this, args);
        };
    if (arguments.length > 2 && params.constructor != Array) {
        params = $.makeArray(arguments).slice(1);
    }
    if (params.constructor != Array) {
        params = [params];
    }
    $.each(params, function (i, n) {
        source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
    });
    return source;
};
//String.prototype.format = function () { if (arguments.length == 0) { return this; } var str = this; for (var i = 0; i < arguments.length; i++) { var re = new RegExp('\\{' + i + '\\}', 'gm'); str = str.replace(re, arguments[i]); } return str; }
//#endregion 
//#region url参数替换，特殊场景使用
//url参数替换，特殊场景使用
function UrlReplace(url, objArr) {
    var obj = '',
        pairs = url.split('&'),
        d = decodeURIComponent,
        name,
        value;
    $.each(pairs, function (i, pair) {
        pair = pair.split('=');
        name = d(pair[0]);
        value = d(pair[1]);
        //        obj[name] = overwrite || !obj[name] ? value :
        //                    [].concat(obj[name]).concat(value);
        var isRemove = false;
        for (var i in objArr) {
            if (name == objArr[i]) {
                isRemove = true;
                break;
            }
        }
        if (!isRemove) {
            obj += name + '=' + value + '&';
        }
    });
    if (obj.length > 0) obj = obj.substring(0, obj.length - 1);
    return obj;
}
//#endregion 
//#region  Js获取当前时间并格式化
function getNowFormatDate() {
    var date = new Date();
    var hs = date.getHours();
    if (hs <= 9) {
        hs = "0" + hs;
    }
    var ms = date.getMinutes();
    if (ms <= 9) {
        ms = "0" + ms;
    }
    var ss = date.getSeconds();
    if (ss <= 9) {
        ss = "0" + ss;
    } 
    return date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + hs + ":" + ms + ":" + ss;;
}
function getFormatDate(longtime) {
    if (longtime == 0) return "";
    var day2 = new Date(longtime * 1000);
    var hs = day2.getHours();
    if (hs <= 9) {
        hs = "0" + hs;
    }
    var ms = day2.getMinutes();
    if (ms <= 9) {
        ms = "0" + ms;
    }
    var ss = day2.getSeconds();
    if (ss <= 9) {
        ss = "0" + ss;
    }
    return day2.getFullYear() + "/" + (day2.getMonth() + 1) + "/" + day2.getDate() + " " + hs + ":"
    + ms + ":" + ss;    
}
//#endregion
